package com.task;
import com.alibaba.fastjson2.JSON;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * author:hlc
 * date:2023/10/4
 *
 * @author 17930
 */
public class ReadFile implements Runnable {
   /**
    * 使用线程池读取json文件
    */
   @Override
   public void run() {
      BufferedReader reader = null;
      try {
         reader = new BufferedReader(new FileReader("resources/info.json"));
      } catch (FileNotFoundException e) {
         throw new RuntimeException(e);
      }
      String line;
      StringBuilder sb = new StringBuilder();
      try {
         line = reader.readLine();
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
      while (line != null) {
         sb.append(line.trim());
         try {
            line = reader.readLine();
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
      }
      String jsonStr = sb.toString();
      //将字符串转为java对象
      JsonObject[] myObjects = JSON.parseObject(jsonStr, JsonObject[].class);
      /**
       * 此处拿到json对象，开始发送请求
       */
     /* URL url = null;*/
      for (JsonObject myObject : myObjects) {
         ArrayList<String> list = myObject.getList();
         FTPUploader.uploader(myObject.getUrl(),myObject.getName(),myObject.getPassword(),list);
         /*try {
            url = new URL(myObject.getUrl());
         } catch (MalformedURLException e) {
            throw new RuntimeException(e);
         }
        *//* HttpClient httpClient = HttpClient.newHttpClient();
         HttpRequest request = null;
         HttpResponse<String> http = null;*//*
         try {
           *//* request = HttpRequest.newBuilder(url.toURI()).GET().timeout(Duration.ofSeconds(10)).build();
            http = httpClient.send(request, HttpResponse.BodyHandlers.ofString());*//*
            *//*System.out.println(myObject.getName() + ": 请求成功,状态码为" + http.statusCode());*//*
            ArrayList<String> list = myObject.getList();
            FTPUploader.uploader(myObject.getUrl(),myObject.getName(),myObject.getPassword(),list);
         } catch (Exception e) {
            System.out.println(myObject.getName()+"：请求失败状态码为404");
         }*/
      }
   }
}
